﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZK.Common.Log4
{
    public static class LogManage
    {
        /// <summary>
        /// 写日志err
        /// </summary>
        /// <param name="str"></param>
        public static void WriteErr(string str)
        {
            Log4NetFile.Err(str);
        }
        /// <summary>
        ///  写日志info
        /// </summary>
        /// <param name="str"></param>
        public static void WriteInfo(string str)
        {
            Log4NetFile.Info(str);
        }
        /// <summary>
        ///  写日志warn
        /// </summary>
        /// <param name="str"></param>
        public static void WriteWarn(string str)
        {
            Log4NetFile.Warn(str);
        }

        /// <summary>
        ///  debug
        /// </summary>
        /// <param name="str"></param>
        public static void WriteDebug(string str)
        {
            Log4NetFile.Debug(str);
        }
        /// <summary>
        /// 删除某个时间段前的日志
        /// </summary>
        public static void DelLogs(int time = 30)
        {
            try
            {
                string filePath = RouteHelper.LogsPath;
                if (Directory.Exists(filePath))
                {
                    var directories = Directory.GetDirectories(filePath);
                    foreach (var item in directories)
                    {
                        var info = new DirectoryInfo(item);
                        if (info.Exists)
                        {
                            foreach (var itemFile in info.GetFiles())
                            {
                                if (itemFile.CreationTime < DateTime.Now.AddDays(-time))
                                {
                                    File.Delete(itemFile.FullName);
                                }
                            }
                        }
                        //if (info.Exists && (info.Attributes & FileAttributes.Directory) != 0 &&
                        //    info.CreationTime < DateTime.Now.AddDays(-7))
                        //    Directory.Delete(item, true);
                    }
                }

            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr(ex.ToString());
            }
        }
    }
}
